package com.tb.controller;

import com.tb.model.Items;
import com.tb.model.User;
import com.tb.service.ItemService;
import com.tb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wangguichun
 * @create: 2018-04-08 09:53
 **/
@Controller
public class IndexController {

    @Autowired
    UserService userService;
    @Autowired
    ItemService itemService;

    @RequestMapping("/")
    public String index(Model model) {
        model.addAttribute("name","王雪梦");
        return "index";
    }


    @RequestMapping("les1")
    public String les1(Model model) {

        String s = userService.getName();
        model.addAttribute("name", s);
        return "les1";
    }

    @RequestMapping("les2")
    public String les2(Model model) {
        User user = userService.getUserById(1);
        model.addAttribute("user",user);
        return "les2";
    }


    @RequestMapping("les3")
    public String les3(Model model) {
        List<User> userList = userService.getUserList();
        model.addAttribute("userList", userList);
        return "les3";
    }

    @RequestMapping("les4")
    public String les4(Model model){

        return "les4";
    }

    @RequestMapping("les4_1")
    public String les4_1(Model model,String userName){
        System.out.println(userName);
        model.addAttribute("name",userName);
        return "les4_1";
    }

    @RequestMapping("les5")
    public String les5(){

        return "les5";
    }

    @RequestMapping("les5_1")
    public String les5_1(Model model,User user){
        model.addAttribute("user",user);
        return "les5_1";
    }

    @RequestMapping("les6")
    public String les6(){

        return "les6";
    }

    @RequestMapping("les6_1/{pageId}")
    public String les6_1(@PathVariable("pageId") int pageid){
        System.out.println(pageid);

        return "les6_1";
    }

    @RequestMapping("les7")
    public String les7(){

        return "les7";
    }

    @RequestMapping("les8")
    public String les8(){

        return "les8";
    }

    @RequestMapping("les9")
    public String les9(){

        return "les9";
    }

    @RequestMapping("les9_1")
    @ResponseBody
    public Map<String,Object> les9_1(int userId){
        System.out.println(userId);
        Map<String,Object> map = new HashMap<>();
//        map.put("name","张三");
//        map.put("sex","0");
//        map.put("address","合肥");

        User u = new User();
        u.setUsername("李四");
        u.setSex((byte) 1);
        u.setAddress("安徽");
        map.put("user",u);
        return map;
    }

    @RequestMapping("les10")
    public String les10(){

//        Items items = itemService.getItemsById(1);
        Items items = itemService.getItems();
//        System.out.println(items.getName());


        return "les10";
    }

}
